//
//  GXDatePickerManager.m
//  PetAnimalProJect
//
//  Created by iMac on 2017/11/13.
//  Copyright © 2017年 谷旭. All rights reserved.
//

#import "NKDatePickerManager.h"
#define MAINSCREENWIDTH [UIScreen mainScreen].bounds.size.width
#define MAINSCREENHEIGHT [UIScreen mainScreen].bounds.size.height

@interface NKDatePickerManager()

@property (nonatomic, copy)void (^dateclikeBlock)(NSString *dateStr,NSString *ApiDateStr);

@end
@implementation NKDatePickerManager

+(NKDatePickerManager *)shareDatePicker{
    
    static NKDatePickerManager *sharedDatePickerInstance = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        sharedDatePickerInstance = [[self alloc] init];
    });


    return sharedDatePickerInstance;
    
}

+(instancetype)DatePickerWithClikeAction:(void (^)(NSString *dateStr,NSString *ApiDateStr))dateClikeBlock{
    
    return [[self alloc]initWithClikeAction:dateClikeBlock];

}


-(instancetype)initWithClikeAction:(void (^)(NSString *dateStr,NSString *ApiDateStr))dateClikeBlock{
    
    if(self= [super init]){
    self.frame=CGRectMake(0, MAINSCREENHEIGHT, MAINSCREENWIDTH, 240);
    _dateclikeBlock=dateClikeBlock;
    [self addSubview:self.datePicker];
    }
    return self;
    
}


-(UIDatePicker *)datePicker{
    
    if(!_datePicker){
        
        _datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, MAINSCREENWIDTH, 240)];
        [_datePicker setLocale:[NSLocale currentLocale]];
        _datePicker.datePickerMode = UIDatePickerModeDateAndTime;
        _datePicker.backgroundColor = [UIColor whiteColor];
        [_datePicker setValue:[UIColor blackColor] forKey:@"textColor"];

    }
    return _datePicker;
    
}
- (void)chooseOver{
   
    if(self.dateclikeBlock) {
        
    self.dateclikeBlock([self getShowUIDateStrFromDate:self.datePicker.date], [self getPostApiDateStrFromDate:self.datePicker.date]);
    
    }
}

- (NSString *)getShowUIDateStrFromDate:(NSDate *)date{
    
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    if(_dateType){
        format.dateFormat = _dateType;
    }else{
        format.dateFormat = @"YYYY年MM月dd日 HH:mm";
    }
    
    NSString *dataStr = [format stringFromDate:date];
    return dataStr;
}

-(NSString *)getPostApiDateStrFromDate:(NSDate *)date{
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    if(_dateApiType){
        format.dateFormat = _dateApiType;
    }else{
        format.dateFormat = @"YYYY-MM-dd HH:mm";
    }
    NSString *dataStr = [format stringFromDate:date];
    return dataStr;
    
}

@end
